package com.cuzofu.fs.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuzofu.fs.common.PageUtils;
import com.cuzofu.fs.common.R;
import com.cuzofu.fs.entity.UnitEntity;
import com.cuzofu.fs.service.UnitService;
import com.cuzofu.fs.vo.UnitVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 单体信息
 *
 * @author cuzofu
 * @email cuzofu@qq.com
 * @date 2020-07-10 08:44:49
 */
@RestController
@RequestMapping("/unit")
public class UnitController {
    @Autowired
    private UnitService unitService;

    /**
     * 列表
     */
    @RequestMapping("/page")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = unitService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
        UnitEntity unit = unitService.getById(id);
        return R.ok().put("unit", unit);
    }

    /**
     * 根据项目ID查询单体信息列表
     */
    @RequestMapping("/infoList/{pid}")
    public R infoList(@PathVariable("pid") String pid) {
        QueryWrapper<UnitEntity> qw = new QueryWrapper<>();
        qw.eq("pid", pid);
        List<UnitEntity> list = unitService.list(qw);

        return R.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/create")
    public R create(@RequestBody UnitVO unit) {
        unitService.createUnit(unit);

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UnitEntity unit) {
        unitService.saveOrUpdate(unit);

        return R.ok().put("unit", unit);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UnitEntity unit) {
        unitService.updateById(unit);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        unitService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
